<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $pv = $uv = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $pv[$day] = model('Click')->field('id')->where(['time' => ['<', strtotime($day)]])->count();
            $uv[$day] = count(model('Click')->where(['time' => ['<', strtotime($day)]])->distinct(true)->field('ip')->select());
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';

        $totaluser = count(model('Click')->distinct(true)->field('ip')->select());

        $totalviews = model('Click')->field('id')->count();

        $articlecount = model('Article')->count();
        $categorycount = model('Category')->count();
        $attachmentcount = model('Attachment')->count();

        $articlecountview = model('Click')->where('type', 2)->count();
        $categoryview = model('Click')->where('type', 1)->count();

        $articlecountavg = round($articlecountview / $articlecount, 2);
        $categoryavg = round($categoryview / $categorycount, 2);

        $daytime = strtotime(date("Y-m-d"),time());
        $todayuseruv = count(model('Click')->where('time', '>', $daytime)->distinct(true)->field('ip')->select());
        $todayuserpv = model('Click')->where('time', '>', $daytime)->count();

        $todayarticleuv = count(model('Click')->where(['time' => ['>', $daytime], 'type' => 2])->distinct(true)->field('ip')->select());
        $todayarticlepv = model('Click')->where(['time' => ['>', $daytime], 'type' => 2])->count();

        $todaycategoryuv = count(model('Click')->where(['time' => ['>', $daytime], 'type' => 1])->distinct(true)->field('ip')->select());
        $todaycategorypv = model('Click')->where(['time' => ['>', $daytime], 'type' => 1])->count();


        $this->view->assign([
            'totaluser'        => $totaluser,
            'totalviews'       => $totalviews,
            'articlecount'     => $articlecount,
            'todayuseruv'      => $todayuseruv,
            'todayuserpv'      => $todayuserpv,
            'todayarticleuv'   => $todayarticleuv,
            'todayarticlepv'   => $todayarticlepv,
            'todaycategoryuv'  => $todaycategoryuv,
            'todaycategorypv'  => $todaycategorypv,
            'paylist'          => $pv,
            'createlist'       => $uv,
            'uploadmode'       => $uploadmode,
            'categorycount'    => $categorycount,
            'categoryview'     => $categoryview,
            'categoryavg'      => $categoryavg,
            'attachmentcount'  => $attachmentcount,
            'articlecountview' => $articlecountview,
            'articlecountavg'  => $articlecountavg,
        ]);

        return $this->view->fetch();
    }

}
